{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "ray.ipynb\n",
    "----------------\n",
    "\n",
    "Demonstrate simple ray- mesh queries\n",
    "\"\"\"\n",
    "\n",
    "import trimesh\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# test on a sphere primitive\n",
    "mesh = trimesh.creation.icosphere()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create some rays\n",
    "ray_origins = np.array([[0, 0, -3],\n",
    "                        [2, 2, -3]])\n",
    "ray_directions = np.array([[0, 0, 1],\n",
    "                           [0, 0, 1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# check out the docstring for intersects_location queries\n",
    "print(mesh.ray.intersects_location.__doc__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# run the mesh- ray query\n",
    "locations, index_ray, index_tri = mesh.ray.intersects_location(\n",
    "        ray_origins=ray_origins,\n",
    "        ray_directions=ray_directions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('The rays hit the mesh at coordinates:\\n', locations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('The rays with index: {} hit the triangles stored at mesh.faces[{}]'.format(index_ray, index_tri))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# stack rays into line segments for visualization as Path3D\n",
    "ray_visualize = trimesh.load_path(np.hstack((ray_origins,\n",
    "                                             ray_origins + ray_directions*5.0)).reshape(-1, 2, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# unmerge so viewer doesn't smooth\n",
    "mesh.unmerge_vertices()\n",
    "# make mesh white- ish\n",
    "mesh.visual.face_colors = [255,255,255,255]\n",
    "mesh.visual.face_colors[index_tri] = [255, 0, 0, 255]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create a visualization scene with rays, hits, and mesh\n",
    "scene = trimesh.Scene([mesh,\n",
    "                       ray_visualize])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# show the visualization\n",
    "scene.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
